package com.lylg.picture_share.controller;

import com.lylg.picture_share.entity.User;
import com.lylg.picture_share.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/user")

public class UserController {
    @Autowired
    private UserServiceImpl UserService;


    @RequestMapping("/getalluser")
    public String getAllUser(Model model){
        List<User> users = UserService.selectAllUser();
        model.addAttribute("userList",users);
        return "manager/user";
    }
    @GetMapping("/toadduser")
    public String toAddUser(){
        return "manager/adduser";
    }
    @PostMapping("/adduser")
    public String adduser(User user){
        System.out.println(user);
        Integer integer = UserService.addUser(user);
        return "redirect:/user/getalluser";
    }

    @GetMapping("/toupdateuser/{user_id}")
    public String toUpdateUser(@PathVariable("user_id") Integer id,Model model){
       User users = UserService.selectUserById(id);
        System.out.println(users);
        model.addAttribute("user",users);
        return "updateuser";
    }
    @PostMapping("/updateuser")
    public String updateUser(Model model,User user){
        System.out.println(user);
        UserService.updateUser(user);
        User users = UserService.selectUserById(user.getUser_id());
         model.addAttribute("user",users);
         return "redirect:/user/getalluser";

    }
    @GetMapping("/del/{user_id}")
    public String deleteImage(@PathVariable("user_id") int id){
        UserService.deleteUser(id);
        return "redirect:/user/getalluser";
    }


    @RequestMapping("/updstatus")
    public String doUpdStatus(Integer user_id){

        UserService.updStatus(user_id);

        return "redirect:/user/getalluser";

    }

}
